﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Common;
using Kronos.DatabaseModels;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Text;

namespace Kronos.Classes
{
    public class ScheduleClass
    {
        public struct Column
        {
            public const int Time = 0;
            public const int Monday = 1;
            public const int Tuesday = 2;
            public const int Wednsday = 3;
            public const int Thursday = 4;
            public const int Friday = 5;
        };

        public struct ClassTimeStretch
        {
            public const int _1hour15minutes = 6;
            public const int _2hours45minutes = 12;
            public const int _50minutes = 5;
        };


        private List<ScheduleData> studentsSchedule;
        DataGrid theSchedule;
        LinkedList<Color> ClassBoxColorList;
        int totalGridBoxes;
        private int ColorIndex;
        Label Message;
        TextBox courseTextbox;
        
        public ScheduleClass(DataGrid theSchedule,Label Message,TextBox courseTextbox)//overloaded constructor it accepts just the basic grid from the aspx page
        {
            this.Message = Message;
             this.theSchedule = theSchedule;
             this.courseTextbox = courseTextbox;
            StartSchedule();
            GetAllTheColors();
            SetEventHandlers();
             theSchedule.DataSource = studentsSchedule;
            totalGridBoxes = studentsSchedule.Count - 1;
        }
        
        public void AddAClass()
         {
             studentsSchedule[0].Monday = "SOEN341";
             studentsSchedule[1].Monday = "Lecture H";
             studentsSchedule[2].Monday = "8:45-10:00";
             studentsSchedule[5].Tuesday = "HELLO";
           //  theSchedule.SelectedRow.Cells[2].BackColor = Color.Red;

             saveSchedule();
         }

         public void StartSchedule()// This will intialise the Datagrid placing time values and strings for the days of the week
         {
             TimeSpan start_time = TimeSpan.Parse("8:45");
             TimeSpan interval_time = TimeSpan.Parse("00:15");
             studentsSchedule = new List<ScheduleData>();
             string time;
             string intial_time = start_time.ToString("hh':'mm");
             studentsSchedule.Add(new ScheduleData() { Time = intial_time });
             for (int i = 0; i < 100; i++)
             {

                 if (start_time == TimeSpan.Parse("20:15"))
                     break;
                 start_time = start_time.Add(interval_time);
                 time = start_time.ToString("hh':'mm");
                 Convert.ToString(start_time);
                 studentsSchedule.Add(new ScheduleData() { Time = time });
             }
             saveSchedule();

         }

         private void saveSchedule()
         {

             theSchedule.DataSource = studentsSchedule;
             theSchedule.DataBind();
             theSchedule.CellSpacing = 0;

         }
         protected void GetClassScheduleData()
         {
             //List<testClass> schedule = testClass.GetClassScheduleData();
             //ClassGrid.DataSource = schedule;
             //ClassGrid.DataBind();
             //removes the default border="1" from the generated grid view

             //sets cellspacing to zero
             //ClassGrid.CellSpacing = 0;
         }

         private void GetAllTheColors()
         {
             Random randomColor = new Random();
             ColorIndex = randomColor.Next(0, 50);
             ClassBoxColorList = new LinkedList<Color>();
             List<string> colors = new List<string>();

             foreach (string colorName in Enum.GetNames(typeof(KnownColor)))
             {
                 //cast the colorName into a KnownColor
                 KnownColor knownColor = (KnownColor)Enum.Parse(typeof(KnownColor), colorName);
                 //check if the knownColor variable is a System color
                 if (knownColor > KnownColor.Transparent)
                 {
                     //add it to our list
                     colors.Add(colorName);
                 }
             }
             foreach (string s in colors)
             {

                 ClassBoxColorList.AddLast(Color.FromName(s));

             }

         }
         private void WriteNotification(string msg)
         {

             Message.Visible = true;


             Message.Text = msg;

         }

         public void deleteCourse()
         {

             saveSchedule();
         }


         private void SetEventHandlers()
         {

             theSchedule.ItemDataBound += new DataGridItemEventHandler(this.addCourseHandler);
             theSchedule.DeleteCommand += new DataGridCommandEventHandler(this.deleteACourseHandler);

         }

         public void addCourseHandler(object sender, DataGridItemEventArgs e)
         {
            
             TableCell TimeCells = e.Item.Cells[Column.Time];
             TableCell MondayCells = e.Item.Cells[Column.Monday];
             TableCell TuesdayCells = e.Item.Cells[Column.Tuesday];
             TableCell WednsdayCells = e.Item.Cells[Column.Wednsday];
             TableCell ThursdayCells = e.Item.Cells[Column.Thursday];
             TableCell FridayCells = e.Item.Cells[Column.Friday];
             string time = TimeCells.Text;

             AddClassOnSchedule(TimeCells,MondayCells,"08:45", 75);
             AddClassOnSchedule(TimeCells, TuesdayCells, "11:15", 75);



             
            
         }

         public void deleteACourseHandler(object sender, DataGridCommandEventArgs e)
         {



         }
         public string RenderClassInformation()
         {
             string courseDetails = "Class" + " Code" + Environment.NewLine + "Section" + Environment.NewLine + "Time Span";

             return courseDetails;
         }

         private List<string> IntializeClassTiming(string startTime, int Duration)
         {
             Duration = Duration - 15;
             List<string> timeList = new List<string>();
             List<TimeSpan> templist = new List<TimeSpan>();

             TimeSpan temp = TimeSpan.Parse(startTime);
             templist.Add(temp);
             timeList.Add(startTime);
             while (Duration >= 0)
             {
                 temp = temp.Add(TimeSpan.Parse("00:15"));
                 templist.Add(temp);
                 // timeList.Add(startTime + Convert.ToString(15));
                 Duration = Duration - 15;
             }

             foreach (TimeSpan timevar in templist)
                 timeList.Add(timevar.ToString("hh':'mm"));

             return timeList;

         }

        private bool ClassConflictOnSchedule(TableCell time,TableCell day, string startTime,int Duration)
        {

            List<string> timelist = new List<string>();
            timelist = IntializeClassTiming(startTime, Duration);


            foreach (string s in timelist)
            {
                if (time.Text.Contains(s))
                {
                    if (day.BackColor != null)
                    {
                        return true;//schedule conflict if there is a color

                    }

                }

            }

            return true;






        }

         private void AddClassOnSchedule(TableCell time,TableCell day, string startTime, int Duration)
         {
             ColorIndex++;
             List<string> timelist = new List<string>();
             timelist = IntializeClassTiming(startTime,Duration);

             foreach (string s in timelist)
             {
                 if (time.Text.Contains(s))
                 {
                     day.BorderStyle = BorderStyle.None;
                     day.BackColor = Color.Red;

                 }



             }
             
         }
         public bool IsAValidCourse(string Class)
         {
             //search db using LINQ
             return true;

             return false;

         }

    
    }

}